package com.home.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.home.common.Cache;
import com.home.model.Price;
import com.home.service.PriceService;

@Controller
public class PriceAction {
	@Autowired
	private PriceService priceService;

	@RequestMapping("/admin/price/list.do")
	public String getAll(HttpServletRequest request) {
		List<Price> priceList = this.getPriceService().getList();
		request.setAttribute("list", priceList);
		return "/admin/price/list";
	}

	@RequestMapping("/admin/price/save.do")
	public String getAll(Price price) {
		if (price != null) {
			this.getPriceService().save(price);
		}
		Cache.getInstance().refreshPrice();
		return "redirect:/admin/price/list.do";
	}

	@RequestMapping("/admin/price/get.do")
	public String get(Integer id, HttpServletRequest request) {
		if (id != null) {
			Price price = this.getPriceService().getPrice(id);
			request.setAttribute("obj", price);
		}
		return "/admin/price/obj";
	}

	@RequestMapping("/admin/price/delete.do")
	public String delete(Integer id, HttpServletRequest request) {
		if (id != null) {
			Price p=new Price();
			p.setId(id);
			this.getPriceService().delete(p);
		}
		return "redirect:/admin/price/list.do";
	}
	
	public PriceService getPriceService() {
		return priceService;
	}

	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}

}
